﻿@{
    Page.Title = "Home";

    var db = Database.Open("bakery");
    var products = db.Query("SELECT * FROM Products").ToList();
    var featured = products[new Random().Next(products.Count)];
    var promotions = db.Query("SELECT * FROM Promotions").ToList();
    var baseUrl = HttpContext.Current.Request.Url.GetLeftPart(System.UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath.TrimEnd('/') + "/";
}

<h1>Welcome to the Gaming Store!</h1>

<div id="featuredProduct">
    <img id="featuredProductImg" alt="Featured Product" src="@Href("~/Images/Products/" + featured.ImageName)" height="260" />
    <div id="featuredProductInfo">
        <div id="productInfo">
            <h2>@featured.Name</h2>
            <p class="price">$@string.Format("{0:f}", featured.Price)</p>
            <p class="description">@featured.Description</p>
        </div>
        <div id="callToAction">
           @{
           @AmazonCheckout.GetAmazonButton(
                            @featured.Name,
                            @featured.Description,
                            @featured.Price,
                            @String.Format("{0}{1}{2}", baseUrl, "Images/Products/Thumbnails/", @featured.ImageName),
                            @featured.Weight,
                            categoryName: @featured.AmazonCategoryName,
                            condition: @featured.AmazonCondition)
            }
        </div>
    </div>
</div>

<ul id="products">
    @foreach (var p in products)
    {
        <li class="product">
            <div class="productInfo">
                <h3>@p.Name</h3>
                <img class="product-image" src="@Href("~/Images/Products/Thumbnails/" + p.ImageName)" alt="Image of @p.Name" />
                <p class="description">@p.Description</p>                    
            </div>
            <div class="action">
                @{
                    var upsellingItems = new List<string>();

                    db.Query("SELECT * FROM UpsellingItems")
                        .Where(prod => prod.ProductId == p.Id)
                        .ToList()
                        .ForEach(i => upsellingItems.Add(i.ASIN));

                    var imageUrl = String.Format("{0}{1}{2}", baseUrl, "Images/Products/Thumbnails/", p.ImageName);
                             
                    var singleItemCart = AmazonCheckout.CreateAmazonCart()
                                                .AddItem(p.Name, p.Description, p.Price, imageUrl, 1, p.Sku, categoryName: p.AmazonCategoryName, condition: p.AmazonCondition, promotionId : p.AmazonPromotionId, shippingMethodIds : new string[]{ "shipping-1","shipping-2"})
                                                .AddUpsellingItems(upsellingItems.ToArray())
                                                .AddInclusionCategories(new string[] { "Software" })
                                                .AddExclusionCategories(new string[] { "Books" });

                    var currentPromotion = promotions.Where(promotion => promotion.Id == p.AmazonPromotionId).SingleOrDefault();
                    var price = p.Price;
                    var style = "";
        
                    if (currentPromotion != null)
                    {
                        if (currentPromotion.Type == "Fixed")
                        {
                            singleItemCart.Promotions.AddFixedAmountDiscount(currentPromotion.Id, currentPromotion.Description, currentPromotion.Amount);
                            price -= currentPromotion.Amount;
                            <div class="promotion" style="float:right">$@string.Format("{0:f} off!", currentPromotion.Amount)</div>
                        }
                        else
                        {
                            singleItemCart.Promotions.AddDiscountRate(currentPromotion.Id, currentPromotion.Description, currentPromotion.Amount);
                            price *= (1 - currentPromotion.Amount);
                            <div class="promotion" style="float:right">$@string.Format("{0:p} off!", currentPromotion.Amount)</div>
                        }

                        style = "promotion";                    
                        <div>
                            <span class="price discounted">$@string.Format("{0:f}", p.Price)</span>
                        </div>
                    }
                    else
                    {
                        <div class="price" >
                            &nbsp;
                        </div>
                    }

                    //((ASP.AmazonCheckout.AmazonCart)singleItemCart).DefaultTaxTable.Id = "default";
                    ((ASP.AmazonCheckout.AmazonCart)singleItemCart).DefaultTaxTable.AddRuleForPredefinedRegion(AmazonCheckout.AmazonPredefinedRegion.USContinental48States, 0.06m);
                    ((ASP.AmazonCheckout.AmazonCart)singleItemCart).DefaultTaxTable.AddRuleForUSState("WA", 0.15m, true);
                    ((ASP.AmazonCheckout.AmazonCart)singleItemCart).DefaultTaxTable.AddRuleForUSZipRegion("98004", 0.03m);
                    //((ASP.AmazonCheckout.AmazonCart)singleItemCart).DefaultTaxTable.AddRuleForWorldRegion("AR", 0.05m, true);
                    
                
                    //Shipping methods for products
                    ((ASP.AmazonCheckout.AmazonCart)singleItemCart).ShippingMethods.Add("shipping-1", "Standard shipping")
                        .WithServiceLevel(AmazonCheckout.ServiceLevel.OneDay)
                        .WithItemQuantityBasedShippingRate(5)
                        .WithShipmentBasedShippingRate(10)
                        .WithWeightBasedShippingRate(12)
                        .IncludeCountryRegion("US")
                        .IncludePredefinedRegion(AmazonCheckout.AmazonPredefinedRegion.USFull50States)
                        .IncludeUSZipRegion("98004")
                        .IncludeUSStateRegion("CA")
                        .ExcludeCountryRegion("GB")
                        .ExcludeUSZipRegion("01984")
                        .ExcludeUSStateRegion("AZ");
 
                    ((ASP.AmazonCheckout.AmazonCart)singleItemCart).ShippingMethods.Add("shipping-2", "Express shipping")
                        .WithServiceLevel(AmazonCheckout.ServiceLevel.Expedited)
                        .WithItemQuantityBasedShippingRate(8)
                        .WithShipmentBasedShippingRate(12)
                        .WithWeightBasedShippingRate(15)
                        .IncludePredefinedRegion(AmazonCheckout.AmazonPredefinedRegion.USFull50States)
                        .IncludeUSZipRegion("98004");
                }                
                
                <div class="price @style" >
                    $@string.Format("{0:f}", price)
                </div>
                @AmazonCheckout.GetAmazonButton(singleItemCart)
            </div>
        </li>
    }
</ul>